package com.ruoyi.system.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.EBusinessShareholder;
import com.ruoyi.system.entity.BusinessInfo;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.QiXinBaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.EBusinessInfoMapper;
import com.ruoyi.system.domain.EBusinessInfo;
import com.ruoyi.system.service.IEBusinessInfoService;

import javax.annotation.Resource;

/**
 * 工商照面信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-23
 */
@Service
public class EBusinessInfoServiceImpl implements IEBusinessInfoService
{
    @Autowired
    private EBusinessInfoMapper eBusineqsInfoMapper;
    @Autowired
    QiXinBaoService qiXinBaoService;
    @Autowired
    ISysDictDataService iSysDictDataService;

    /**
     * 查询工商照面信息
     *
     * @param id 工商照面信息主键
     * @return 工商照面信息
     */
    @Override
    public EBusinessInfo selectEBusinessInfoById(Long id)
    {
        return eBusineqsInfoMapper.selectEBusinessInfoById(id);
    }

    /**
     * 查询工商照面信息列表
     *
     * @param eBusinessInfo 工商照面信息
     * @return 工商照面信息
     */
    @Override
    public List<EBusinessInfo> selectEBusinessInfoList(EBusinessInfo eBusinessInfo)
    {
        return eBusineqsInfoMapper.selectEBusinessInfoList(eBusinessInfo);
    }

    /**
     * 新增工商照面信息
     *
     * @param eBusinessInfo 工商照面信息
     * @return 结果
     */
    @Override
    public int insertEBusinessInfo(EBusinessInfo eBusinessInfo)
    {
        return eBusineqsInfoMapper.insertEBusinessInfo(eBusinessInfo);
    }

    /**
     * 修改工商照面信息
     *
     * @param eBusinessInfo 工商照面信息
     * @return 结果
     */
    @Override
    public int updateEBusinessInfo(EBusinessInfo eBusinessInfo)
    {
        return eBusineqsInfoMapper.updateEBusinessInfo(eBusinessInfo);
    }

    /**
     * 批量删除工商照面信息
     *
     * @param ids 需要删除的工商照面信息主键
     * @return 结果
     */
    @Override
    public int deleteEBusinessInfoByIds(Long[] ids)
    {
        return eBusineqsInfoMapper.deleteEBusinessInfoByIds(ids);
    }

    /**
     * 删除工商照面信息信息
     *
     * @param id 工商照面信息主键
     * @return 结果
     */
    @Override
    public int deleteEBusinessInfoById(Long id)
    {
        return eBusineqsInfoMapper.deleteEBusinessInfoById(id);
    }

    @Override
    public BusinessInfo getBusinessInfoByName(String discountApplicantName,LoginUser loginUser) {
        String toName=discountApplicantName;
        SysDictData sysDictData=new SysDictData();
        sysDictData.setDictType("discount_applicant_name");
        List<SysDictData> selectDictDatas=iSysDictDataService.selectDictDataList(sysDictData);
        for(SysDictData sysDict:selectDictDatas){
            if(null!=sysDict&&discountApplicantName.contains(sysDict.getDictLabel())){
                toName=discountApplicantName.replace(sysDict.getDictLabel(),StringUtils.isEmpty(sysDict.getDictValue())?"":sysDict.getDictValue());
                break;
            }
        }
        //if(discountApplicantName.contains("营业部")){
        //    toName=discountApplicantName.replace("营业部","");
        //}else if(discountApplicantName.contains("分理处")){
        //    toName=discountApplicantName.replace("分理处","支行");
        //}else if(discountApplicantName.contains("总行营业部")){
        //    toName=discountApplicantName.replace("总行营业部","");
        //}else if(discountApplicantName.contains("襄阳营业部")){
        //    toName=discountApplicantName.replace("襄阳营业部","襄阳分行");
        //}else if(discountApplicantName.contains("工行")){
        //    toName=discountApplicantName.replace("工行","工商");
        //}else if(discountApplicantName.contains("专业处理中心")){
        //    toName=discountApplicantName.replace("专业处理中心","");
        //}
        EBusinessInfo eBusinessInfo=getBusinessInfoByNameScope(discountApplicantName,loginUser);
        BusinessInfo businessInfo=new BusinessInfo();
        if(null==eBusinessInfo){
            eBusinessInfo=new EBusinessInfo();
            businessInfo=qiXinBaoService.getBusinessInfoByName(toName,loginUser);
            if(StringUtils.isNotEmpty(businessInfo.getStatus())&&"200".equals(businessInfo.getStatus())){
                //请求启信宝接口
                eBusinessInfo.setBusinessName(discountApplicantName);
                eBusinessInfo.setBusinessCode("");
                eBusinessInfo.setBusinessMsg(JSON.toJSONString(businessInfo));
                eBusinessInfo.setUserId(loginUser.getUserId());
                eBusinessInfo.setDeptId(loginUser.getDeptId());
                insertEBusinessInfo(eBusinessInfo);
            }
        }else{
            businessInfo=JSON.parseObject(eBusinessInfo.getBusinessMsg(),BusinessInfo.class);
        }
        return businessInfo;
    }

    public EBusinessInfo getBusinessInfoByNameScope(String discountApplicantName,LoginUser loginUser){
        EBusinessInfo eBusinessInfo=new EBusinessInfo();
        eBusinessInfo.setBusinessName(discountApplicantName);
        eBusinessInfo.setDeptId(loginUser.getDeptId());
        return  eBusineqsInfoMapper.getBusinessInfoByName(eBusinessInfo);
    }
}
